<?php
namespace app\home\model;

//加载配置类
use think\Config;
//导入Controller
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class OrdersModel extends Model
{
	/**
	*	确认订单
	*  @param  array  $ids  购物车的id
	*/
	public function makeOrder($ids)
	{
		$where = [];
		if($ids){
			$user_id = Session::get('user_id');
			$where['c.uid'] = $user_id;
			$result = Db::table('sz_cart')
					->alias('c')
					->join('sz_goods g','c.gid=g.id','left')
					->field('c.*,g.g_name,g.g_pic,g.g_description,g.g_unit')
					->where('c.id','in',"{$ids}")
					->where($where)
					->select();

			$res  = [];
			$arr  = [];
			$data = [];
			foreach($result as $key=>$val){
				$list[$key] = json_decode($val['g_pic'],true);
				foreach($list as $k1=>$v1){
					$res[$key]['photos'] = $v1['photos'];
					foreach($res[$key]['photos'] as $k2=>$v2){
						$arr['url'] = getPreviewUrl($v2['url']);
					}
				}

				$data[$key]['info'] = Db::table('sz_cartinfo')->alias('ci')
									->join('sz_info i','ci.iid=i.id','left')
									->field('ci.*,i.i_name,i.i_price,i.i_number,i.i_commission')
									->where('ci.tid',$val['id'])
									->select();

				$data[$key]['g_pic'] = $arr;
				$data[$key]['id'] = $val['id'];
				$data[$key]['g_name'] = $val['g_name'];
				$data[$key]['g_description'] = $val['g_description'];
				$data[$key]['g_unit'] = $val['g_unit'];
				$data[$key]['t_total'] = $val['t_total'];
				$data[$key]['gid'] = $val['gid'];

			}

			return $data;
		}
	}

	/**
	*	添加订单-订单信息
	*/
	public function addUserOrders($data)
	{
		$map = [];
		if($data){
			/* 先添加下面字段到,总金额后面再加 */
			$map['uid'] = Session::get('user_id');

			if($data['o_type'] == '1'){

				/* 获取用户的余额 */
				$result = Db::table('sz_user')->where('id',$map['uid'])->find();
				$balance = $result['u_wallet'] - $data['o_tprice'];
				Db::table('sz_user')->where('id',$map['uid'])->update(['u_wallet'=>$balance]);
				$map['o_status'] = 2;
				$map['o_type'] = $data['o_type'];
				$map['o_time'] = strtotime(date('Y-m-d H:i',time()));

				/* 订单号 */
				$map['gids'] = $data['gids'];
				$map['o_code'] = getOrderCode();
				$map['o_tprice'] = $data['o_tprice'];
				$map['o_message'] = isset($data['o_message']) ? $data['o_message'] : '';
				$map['aid'] = $data['aid'];
				$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
				$res = Db::table('sz_orders')->insertGetId($map);
				/* 用户的余额支出 */
				Db::table('sz_spending')->insert(['s_profit'=>$data['o_tprice'],'oid'=>$res,'uid'=>$map['uid']]);
				return $res;
			}
			// }elseif($data['o_type'] == '2'){
			// 	$map['o_status'] = 2;
			// 	$map['o_type'] = $data['o_type'];
			// 	$map['o_time'] = strtotime(date('Y-m-d H:i',time()));
			// 	/* 订单号 */
			// 	$map['gids'] = $data['gids'];
			// 	$map['o_code'] = getOrderCode();
			// 	$map['o_tprice'] = $data['o_tprice'];
			// 	$map['o_message'] = isset($data['o_message']) ? $data['o_message'] : '';
			// 	$map['aid'] = $data['aid'];
			// 	$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
			// 	$res = Db::table('sz_orders')->insertGetId($map);
			// 	return $res;

			// }elseif($data['o_type'] == '3'){
			// 	$map['o_status'] = 2;
			// 	$map['o_type'] = $data['o_type'];
			// 	$map['o_time'] = strtotime(date('Y-m-d H:i',time()));
			// 	/* 订单号 */
			// 	$map['gids'] = $data['gids'];
			// 	$map['o_code'] = getOrderCode();
			// 	$map['o_tprice'] = $data['o_tprice'];
			// 	$map['o_message'] = isset($data['o_message']) ? $data['o_message'] : '';
			// 	$map['aid'] = $data['aid'];
			// 	$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
			// 	$res = Db::table('sz_orders')->insertGetId($map);
			// 	return $res;
			// }

			/* 订单号 */
			$map['gids'] = $data['gids'];
			$map['o_code'] = getOrderCode();
			$map['o_tprice'] = $data['o_tprice'];
			$map['o_message'] = isset($data['o_message']) ? $data['o_message'] : '';
			$map['aid'] = $data['aid'];
			$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
			$res = Db::table('sz_orders')->insertGetId($map);
			return $res;

		}
	}

	/**
	*	添加订单-商品信息
	*/
	public function addOrdersInfo($ids,$oid)
	{
		if($ids){
			$uid = Session::get('user_id');
			$result = Db::table('sz_cartinfo')->where('tid','in',"{$ids}")->select();
			$data = [];
			foreach($result as $key=>$val){
				$data['oid'] = $oid;
				$data['gid'] = $val['gid'];
				$data['iid'] = $val['iid'];
				$data['oi_number'] = $val['ci_number'];
				$data['oi_price'] = $val['ci_total'];
				Db::table('sz_orderinfo')->insert($data);
			}

			/* 清空购物车 */
			Db::table('sz_cart')->where('id','in',"{$ids}")->delete();
			Db::table('sz_cartinfo')->where('tid','in',"{$ids}")->delete();

			return true;

		}else{

			return false;
		}
	}

	/**
	*	获取一条订单详情页
	*   @param $id
	*/
	public function getUserNorders($id)
	{
		if($id){
			$list = Db::table('sz_orders')->where('id',$id)->find();
			$arr = explode(',', $list['gids']);
			$res = [];
			foreach($arr as $key=>$val){
				$data['goods'][$key] = Db::table('sz_goods')->where('id',$val)->find();
				$res = $data['goods'];
				foreach($res as $k1=>$v1){
					$list['goods'][$key]['id'] = $v1['id'];
					$list['goods'][$key]['g_name'] = $v1['g_name'];
					$list['goods'][$key]['g_description'] = $v1['g_description'];
					$list['goods'][$key]['g_pic'] = json_decode($v1['g_pic'],true);
					$list['goods'][$key]['info'] = Db::table('sz_orderinfo')->alias('o')
									->join('sz_info i','o.iid=i.id','left')
									->field('o.*,i.i_name,i.i_number,i.i_price,i.i_commission')
									->where(['o.gid'=>$v1['id'],'o.oid'=>$id])->select();
				}

			}

			/* 收货地址 */
			$address = Db::table('sz_address')->where('id',$list['aid'])->find();
			return ['res'=>$list,'address'=>$address];
		}
	}

	/**
	*	付款成功页
	*/
	public function OrderSuccess($id)
	{
		if($id){
			$res = Db::table('sz_orders')->where('id',$id)->find();
			return $res;
		}
	}


	/**
	*	未付款的订单-支付
	*  @param $id  订单id
	*  @param $data  数据
	*/
	public function orderPay($id,$data)
	{
		$map = [];
		if($data){
			$uid = Session::get('user_id');
			if($data['o_type'] == '1'){
				$map['o_status'] = 2;
				$map['o_type'] = $data['o_type'];
				$map['o_time'] = strtotime(date('Y-m-d H:i',time()));
				$res = Db::table('sz_orders')->where('id',$id)->update($map);
				$result = Db::table('sz_orders')->where('id',$id)->find();
				Db::table('sz_spending')->insert(['s_profit'=>$result['o_tprice'],'oid'=>$id,'uid'=>$uid]);
				return true;

			}elseif($data['o_type'] == '2'){
				$map['o_status'] = 2;
				$map['o_type'] = $data['o_type'];
				$map['o_time'] = strtotime(date('Y-m-d H:i',time()));
				$res = Db::table('sz_orders')->where('id',$id)->update($map);
				return true;

			}elseif($data['o_type'] == '3'){
				$map['o_status'] = 2;
				$map['o_type'] = $data['o_type'];
				$map['o_time'] = strtotime(date('Y-m-d H:i',time()));
				$res = Db::table('sz_orders')->where('id',$id)->update($map);
				return true;
			}

		}else{
			return false;
		}
	}


	/**
	*	取消订单
	* @param $id  订单id
	*/
	public function deleteOrder($id)
	{
		$map = [];
		if($id){
			$map['o_status'] = 3;
			$map['o_time'] = strtotime(date('Y-m-d H:i',time()));
			Db::table('sz_orders')->where('id',$id)->update($map);
			return true;
		}else{
			return false;
		}
	}
}